package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.constant.CommonConst;
import com.guodi.model.entity.ApiMapLayer;
import com.guodi.model.entity.MdlModel;
import com.guodi.model.mapper.MdlModelMapper;
import com.guodi.model.service.IMdlModelResultService;
import com.guodi.model.service.IMdlModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * @描述：模型信息表控制类
 * @作者: 代鹏刚
 * @日期: 2020-06-17
 */
@RestController
@RequestMapping("/MdlModel")
@Api(value = "API - MdlModelController", description = "模型信息表")
public class MdlModelController extends BaseController {

    @Resource
    public IMdlModelService mdlModelService;
    @Resource
    public IMdlModelResultService mdlModelResultService;
    @Resource
    private MdlModelMapper mdlModelMapper;

    /**
    * @描述: 根据实体属性分页查询
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-06-17
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = true, dataType = "long", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "keyWord", value = "关键字搜索", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortType", value = "排序类型(1:时间 2:使用次数)", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortMethod", value = "排序方式(1:向下 2:向上)", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult listByEntity(@ApiIgnore Page page, @ApiIgnore MdlModel mdlModel, @ApiIgnore CommonConst commonConst){
        IPage<MdlModel> mdlModels = mdlModelService.listByEntity(page,mdlModel,commonConst);
        return new AjaxResult(mdlModels);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id) {
        if(StringUtils.isBlank(id)){
            return new AjaxResult(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MdlModel mdlModel = mdlModelService.getById(id);
        if(StringUtils.isNotBlank(mdlModel.getSampleLayerUrl())){
            ApiMapLayer mapLayer = mdlModelMapper.listMapLayerByMapLayerId(mdlModel.getSampleLayerUrl());
            if (mapLayer != null) {
                mdlModel.setSampleLayerInfo(mapLayer);
            } else {
                mdlModel.setSampleLayerInfo(new ApiMapLayer());
            }
        }else {
            mdlModel.setSampleLayerInfo(new ApiMapLayer());
        }
        if(StringUtils.isNotBlank(mdlModel.getResultLayer())){
            ApiMapLayer mapLayer = mdlModelMapper.listMapLayerByMapLayerId(mdlModel.getResultLayer());
            if (mapLayer != null) {
                mdlModel.setResultLayerInfo(mapLayer);
            } else {
                mdlModel.setResultLayerInfo(new ApiMapLayer());
            }
        }else {
            mdlModel.setResultLayerInfo(new ApiMapLayer());
        }
        mdlModel.setHistoryDataYear(mdlModelResultService.getHistoryYearData(mdlModel.getId()));
        return new AjaxResult(mdlModel);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-04-01
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "模型名称", required = true,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "模型数据类型", required = true,  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sampleLayerUrl", value = "样例图层地址",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "模型描述", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "calculate", value = "模型计算", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "introduce", value = "模型介绍",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "levelStandard", value = "分级标准",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "calculateProcess", value = "计算流程",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "config", value = "模型因子配置信息",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "useCount", value = "使用次数",  dataType = "string", paramType = "query")
    })
    public AjaxResult save(@ApiIgnore MdlModel mdlModel){
        prepareData(mdlModel, true);
        mdlModel.setId(UuidUtil.get32UUID());
        mdlModelService.save(mdlModel);
        return new AjaxResult(mdlModel);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模型名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "模型数据类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sampleLayerUrl", value = "样例图层地址",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "模型描述",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "calculate", value = "模型计算",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "introduce", value = "模型介绍",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "levelStandard", value = "分级标准",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "calculateProcess", value = "计算流程",  dataType = "string", paramType = "query")
    })
    public AjaxResult update(@ApiIgnore MdlModel mdlModel) {
        prepareData(mdlModel, false);
        mdlModelService.updateById(mdlModel);
        return new AjaxResult();
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个使用逗号隔开", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String ids){
        String[] id = ids.split(",");
        mdlModelService.removeByIds(Arrays.asList(id));
        return new AjaxResult();
    }

    /**
    * @描述: 获取模型列表数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/6/19 17:37
    **/
    @PostMapping("/getList")
    @ApiOperation(value = "获取模型列表数据")
    public AjaxResult getList(){
        List<MdlModel> mdlModels = mdlModelService.list();
        return new AjaxResult(mdlModels);
    }

    private void prepareData(MdlModel mdlModel, boolean isSave) {
        if(isSave){
            mdlModel.setCreator(this.getUser().getUserId().toString());
            mdlModel.setCreateTime(new Date());
            //防止误修改
            mdlModel.setModifier(null);
            mdlModel.setModifyTime(null);
        }else{
            mdlModel.setModifier(this.getUser().getUserId().toString());
            mdlModel.setModifyTime(new Date());
            //防止误修改
            mdlModel.setCreator(null);
            mdlModel.setCreateTime(null);
        }
    }
}
